package com.bocai.kelalacar.ui.car.set.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.BailOrderBean;
import com.bocai.kelalacar.bean.ConfigBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.set.contract.PwSetContract;
import com.bocai.kelalacar.ui.car.set.contract.ServiceMarginContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/29.
 */

public class ServiceMarginPresenter extends ServiceMarginContract.presenter {

    private final ServiceMarginContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public ServiceMarginPresenter(ServiceMarginContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void bailOrder() {
        view.showLoading("");
        api.bailOrder().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result =  AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    BailOrderBean bean = new Gson().fromJson(result,BailOrderBean.class);
                    view.bailOrderSuccess(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void orderPay(String type,String orderId) {
        view.showLoading("");
        api.payType(type,orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = baseResponse.getData();
                    Log.e("result",result);
                    view.orderPaySuccess(result);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getConfig() {
        view.showLoading("");
        api.config().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result =  AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    ConfigBean bean = new Gson().fromJson(result,ConfigBean.class);
                    view.getConfig(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
